/*
 * @Description:
 * @Autor: Bingo
 * @Date: 2022-12-12 10:43:49
 * @LastEditors: Bingo
 * @LastEditTime: 2024-06-13 10:17:19
 */
import { Router } from "vue-router";
import { useLogin } from "@/store/login";
const routeSafe = (router: Router) => {
  /* 
    路由守卫  每次切换url都会触发 哪怕是本身
    这里仅做了登录处理
  */
  router.beforeEach((to, from, next) => {
    document.documentElement.scrollTop = 0;
    let envType = localStorage.getItem("tk");
    if (envType && envType != "{}") {
      // 登录状态 如果是前往登录页则跳首页
      if (to.path == "/login") {
        next({
          path: "/",
        })
      }else{
          next();
      }
      
    } else {
      // 非登录状态 如果是前往登录页则直接放行
      if (to.path == "/login") {
        next();
        // 如果是其他页面则跳到登录页
      } else {
        next({
          path: "/login",
        });
      }
    }
  });
  return router;
};

export default routeSafe;
